package xyz.chenyongpeng.userconsumerdubbo.service;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.chenyongpeng.entity.ZsSignIn;
import xyz.chenyongpeng.entity.ZsUser;
import xyz.chenyongpeng.entity.ZsUserTrail;
import xyz.chenyongpeng.service.IZsCouponService;
import xyz.chenyongpeng.service.IZsSignInService;
import xyz.chenyongpeng.service.IZsUserService;
import xyz.chenyongpeng.service.IZsUserTrailService;
import xyz.chenyongpeng.utils.Rid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private IZsUserService zsUserService;

    @Autowired
    private IZsCouponService zsCouponService;

    @Autowired
    private IZsSignInService zsSignInService;

    @Autowired
    private IZsUserTrailService zsUserTrailService;

    public String saveOrUpdate(String wx_openid,String u_name,String u_nickname,String u_phone,String u_img){
        Map<String,Object> parms = new HashMap<>();
        parms.put("openid", wx_openid);
        if (!StringUtils.isEmpty(u_name)){
            parms.put("u_name", u_name);
        }
        if (!StringUtils.isEmpty(u_nickname)){
            parms.put("u_nickname", u_nickname);
        }
        if (!StringUtils.isEmpty(u_phone)){
            parms.put("u_phone", u_phone);
        }
        if (!StringUtils.isEmpty(u_img)){
            parms.put("u_img", u_img);
        }
        return zsUserService.addUser(parms);
    }

    public ZsUser get(String openid){
        return zsUserService.getInfoByOpenid(openid);
    }

    public int getCouponLostCount(String u_id){
        return zsCouponService.getCouponLost(u_id);
    }

    public String getUid(String openid){
        return zsUserService.getUidByOpenid(openid);
    }

    public int signResource(String r_id,String u_id,long sign_in_time){
        ZsSignIn zsSignIn = new ZsSignIn();
        zsSignIn.setRId(Integer.parseInt(r_id));
        zsSignIn.setUId(Integer.parseInt(u_id));
        zsSignIn.setSignInTime((int)sign_in_time);
        return zsSignInService.signResource(zsSignIn);
    }

    public boolean reprot(long u_id,String wx_openid,float longitude,float latitude){
        JSONObject p = new JSONObject();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user_id", u_id);
        jsonObject.put("lng", longitude);
        jsonObject.put("lat", latitude);
        p.put("data", jsonObject);
        String data = p.toString();

        String url = "http://transform.hcmis.cn/GeoService/transform";

        String httpResult = HttpUtil.post(url, data);
        Rid rid = JSONUtil.toBean(httpResult, Rid.class);

        ZsUserTrail zsUserTrail = new ZsUserTrail();
        zsUserTrail.setLongitude(longitude+"");
        zsUserTrail.setLatitude(latitude+"");
        zsUserTrail.setWxOpenid(wx_openid);
        zsUserTrail.setReTime((int)(System.currentTimeMillis() /1000));
        zsUserTrail.setUId((int)u_id);
        zsUserTrail.setFixedX(rid.getResult().getX()+"");
        zsUserTrail.setFixedY(rid.getResult().getY()+"");
        return zsUserTrailService.save(zsUserTrail);
    }

    public List<Integer> getSignIn(String u_id){
        List<ZsSignIn> zsSignIns = zsSignInService.list(new QueryWrapper<ZsSignIn>().eq("u_id", u_id));
        List<Integer> rids = new ArrayList<>();
        for (ZsSignIn sign: zsSignIns) {
            rids.add(sign.getRId());
        }
        return rids;
    }
}
